package com.sunkyc.framework.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunkyc.commons.entity.PageQueryVo;
import com.sunkyc.commons.entity.TablePageData;
import com.sunkyc.framework.system.entity.SysReqLog;
import com.sunkyc.framework.system.mapper.SysReqLogMapper;
import com.sunkyc.framework.system.service.SysReqLogService;
import com.sunkyc.framework.utils.TablePageDataBuilderUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 操作日志记录 服务实现类
 * </p>
 *
 * @author sunkyc
 * @since 2022-06-12
 */
@Service
@RequiredArgsConstructor
public class SysReqLogServiceImpl extends ServiceImpl<SysReqLogMapper, SysReqLog> implements SysReqLogService {
    private final SysReqLogMapper baseMapper;

    @Override
    public TablePageData<SysReqLog> queryList(PageQueryVo pageQueryVo, SysReqLog sysReqLog) {
        Page<SysReqLog> page = new Page<>(pageQueryVo.getCurrent(),pageQueryVo.getPageSize());
        IPage<SysReqLog> iPage = baseMapper.selectPage(page,buildWrapper(sysReqLog));
        return TablePageDataBuilderUtil.buildTablePageData(iPage);
    }

    @Override
    public int deleReqLog() {
        return baseMapper.delete(null);
    }

    private Wrapper<SysReqLog> buildWrapper(SysReqLog sysReqLog){
        return Wrappers.lambdaQuery(SysReqLog.class)
                .eq(ObjectUtil.isNotEmpty(sysReqLog.getCreateBy()),SysReqLog::getCreateBy,sysReqLog.getCreateBy())
                .eq(ObjectUtil.isNotNull(sysReqLog.getReqState()),SysReqLog::getReqState,sysReqLog.getReqState())
                .orderByDesc(SysReqLog::getReqTime);
    }
}
